<template>
  <div>
    <h3>树形数据转换</h3>
  </div>
</template>
<script>
export default {
  created() {
    const arrList = [
      { id: 1, name: '广东', pid: '' },
      { id: 2, name: '深圳', pid: 1 },
      { id: 3, name: '宝安', pid: 2 },
      { id: 4, name: '广西', pid: '' }
    ]
    // 1:过滤出第一层数据
    function changeData(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = changeData(arrList, item.id)
          return true
        }
      })
      // 返回值=数组.filter(item=>{return boolean  true  返回值就添加当前item})
    }
    // console.log(changeData(arrList, ''))
    // 2:找出某一项广东的子集
    // console.log(changeData(arrList, 1))
    // 3：给第一层每一项加个children []
    // 4:递归调用  item.children=changeData(arrList, item.id)
    console.log(changeData(arrList, ''))
  }
}
</script>
<style></style>
